package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result page(SetmealPageQueryDTO dto) {
        log.info("套餐分页查询:{}", dto);
        PageResult pageResult = setmealService.pageQuery(dto);
        return Result.success(pageResult);
    }

    @CacheEvict(cacheNames = {"setmealCache"}, allEntries = true)
    @PostMapping
    @ApiOperation("新增套餐")
    public Result save(@RequestBody SetmealDTO dto) {
        log.info("新增套餐：{}", dto);
        setmealService.save(dto);
        return Result.success();

    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询套餐信息")
    public Result queryById(@PathVariable("id") Long id) {
        log.info("根据ID查询套餐信息：{}", id);
        SetmealVO setmealVO = setmealService.queryById(id);
        return Result.success(setmealVO);

    }

    @CacheEvict(cacheNames = {"setmealCache"}, allEntries = true)
    @PutMapping
    @ApiOperation("修改套餐")
    public Result update(@RequestBody SetmealDTO dto) {
        log.info("修改套餐：{}", dto);
        setmealService.update(dto);
        return Result.success();

    }

    @CacheEvict(cacheNames = {"setmealCache"}, allEntries = true)
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    public Result batchDelete(@RequestParam List<Long> ids) {
        log.info("批量删除套餐：{}", ids);
        if (ids == null || ids.size() <= 0) {
            return Result.error("请选择要删除的商品");
        }
        setmealService.batchDelete(ids);
        return Result.success();

    }

    /**
     * 使用路径参数 需要注意 /status/{status}
     *
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = {"setmealCache"}, allEntries = true)
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售、停售")
    public Result enableOrDisable(@PathVariable("status") int status, Long id) {
        log.info("套餐起售、停售：{},{}", status, id);
        setmealService.enableOrDisable(status, id);
        return Result.success();
    }

}
